﻿//========================================================= 
/**@file PCSC_Installer.h
 * @brief PCSC协议CCID设备的驱动
 * 
 * @date 2018-07-28   09:19:28
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_PCSC_INSTALLER_H_
#define _LIBZHOUYB_PCSC_INSTALLER_H_
//--------------------------------------------------------- 
#include "../module/ComplexCmdInstaller.h"

#include "../module/PBOC_CmdDriver.h"

#include "../../pboc/pboc_app.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
//--------------------------------------------------------- 
/// PCSC驱动
class PCSC_Installer : public ComplexCmdInstaller
{
public:
    PCSC_Installer() : ComplexCmdInstaller() {}
    PCSC_Installer(CommandRunner& runner) : ComplexCmdInstaller(runner) {}
    /**
     * @brief 注册新的ITransceiveTrans接口设备(默认选择最后的ITransceiveTrans)
     * @date 2018-07-28 09:58
     * 
     * @param [in] devId 需要注册的设备ID
     */
    template<class T>
    CommandSetter InstallDEV(const char* devId)
    {
        if(_runner.IsNull() || _is_empty_or_null(devId))
            return CommandSetter();

        CommandSetter devSetter = _runner->template New<T>(devId);
        const char DEV_BIND_CMD[] = "ITransceiveTrans";
        CommandSetter setter(_runner);
        setter.New(DEV_BIND_CMD);
        setter.New("DEV.ITransceiveTrans");

        // 注册选择设备的命令 ActiveSLOT.DEV
        string scriptName = "Script.";
        scriptName += devId;
        scriptName += ".OpenBindCMD";

        Ref<CommandScriptActuator> devScript = NewScriptCMD(devId);
        _BindCMD(devScript, devId, "", DEV_BIND_CMD);
        _BindCMD(devScript, devId, "DEV", "ITransceiveTrans");

        devScript->Run(devSetter.LookUp("IBaseDevice"))
            .ToArg().Run(_runner->Registe("Select_IBaseDevice"))
            .Clear(true, true)
            .Run(_runner->LookUp("ITransceiveTrans"))
            .ToArg().Run(_runner->Registe("Select_ITransceiveTrans"));
        string name = "ActiveSLOT.";
        name += devId;
        setter.New(name.c_str()).Bind(devScript->Script(scriptName.c_str()));
        return devSetter;
    }
    /**
     * @brief 装载PBOC模块
     * @date 2018-07-28 10:08
     */
    void Install(const char* devId = NULL)
    {
        if(_runner.IsNull())
            return;
        CommandSetter devSetter(*_runner);
        const char DEV_ID[] = "";
        Ref<ComplexCommand> complexCmd = _runner->Registe("ITransceiveTrans");
        if(!_is_empty_or_null(devId))
        {
            Ref<CommandCollection> pDevCollection = _runner->Item(devId);
            if(!pDevCollection.IsNull())
            {
                devSetter.Set(*pDevCollection);
                complexCmd->Clear();
                complexCmd->Bind(devSetter.LookUp("ITransceiveTrans"));
            }
        }
        else
        {
            devId = DEV_ID;
        }
        CommandSetter setter = (*_runner);
        // PBOC
        CommandInstaller::RegisteNew<PBOC_CmdDriver>(*_runner, "PBOC");

        // 创建 Select_ITransceiveTrans 接口
        const char SELECT_DEVICE[] = "SelectDevice";
        Ref<ComplexCommand> selectCmd = _runner->Registe("Select_ITransceiveTrans");
        selectCmd->Bind(_runner->Get("PBOC").LookUp(SELECT_DEVICE), ComplexCommand::RunOnAlways);
        _runner->LastErr().New("").Add("PBOC").End().Add(devId);
    }
};
//--------------------------------------------------------- 
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_PCSC_INSTALLER_H_
//========================================================= 